<?php

  $pct->setParam('year', $_GET['_a2']);
  $pct->setParam('month', $_GET['_a3']);
  $pct->setParam('day', $_GET['_a4']);
  $pct->setParam('news', $_GET['_a5']);

  if(!isBlank($pct->getParam('year')) && !isBlank($pct->getParam('month')) && !isBlank($pct->getParam('day')) && !isBlank($pct->getParam('news')))
  {     $SameURL = $BaseURL.substr(getConstUri(),1);

     $ErrorMessages = array();
	if($_POST['nact'] == 'cmmnt')
	{
	  if(isBlank($_POST['body']))
	  {
		$ErrorMessages[] = $ERRORS['contacts'][__ERROR_EMPTY_REQUIRED_FIELDS];
	  }
	  if(isBlank($_POST['code']) || $_POST['code'] != getSecurityNumber())
	  {
		$ErrorMessages[] = $ERRORS['contacts'][__ERROR_INCORRECT_SECURITY_CODE];
	  }
	  if(isEmptyArr($ErrorMessages))
	  {
	     $query = "
	  			SELECT n.id
	  			FROM ".$_SQL_TABLE['news']." n
	 			WHERE
	 					n.active = 'y'
	 				AND n.linkname = ".$dbSet->quoteValue($pct->getParam('news'))."
	 				AND n.p_date >= '".$pct->getParam('year')."-".$pct->getParam('month')."-".$pct->getParam('day')."' AND n.p_date <= '".$pct->getParam('year')."-".$pct->getParam('month')."-".$pct->getParam('day')." 23:59:59'
	    ";
	    $dbSet->open($query);
	    $newsId = $dbSet->loadResult();

	    $toDBArr = array();
        $toDBArr['root'] = $_POST['root'];
        $toDBArr['body'.__FLANG] = $_POST['body'];
        $toDBArr['active'] = 'm';
        $toDBArr['news_id'] = $newsId;
        $toDBArr['user'] = $USER['info']['id'];
        $toDBArr['date'] = 'NOW()';
        makeInsertList($strColumns, $strValues, $toDBArr, array('date'));
	    insertItem($_SQL_TABLE['comment'], $strColumns, $strValues);

	    showMessageAlertJVSC($language['front']['thankYouForYourComment']);
	    go_toJVSC($SameURL);
	    exit;
	  }
	}

	if($_POST['gact'] == 'rtng')
	{
	  $query = "
	  			SELECT n.id, np.news_id, np.rating_sum, np.rating_count
	  			FROM ".$_SQL_TABLE['news']." n
	 			LEFT JOIN ".$_SQL_TABLE['news_param']." np ON np.news_id = n.id
	 			WHERE
	 					n.active = 'y'
	 				AND n.linkname = ".$dbSet->quoteValue($pct->getParam('news'))."
	 				AND n.p_date >= '".$pct->getParam('year')."-".$pct->getParam('month')."-".$pct->getParam('day')."' AND n.p_date <= '".$pct->getParam('year')."-".$pct->getParam('month')."-".$pct->getParam('day')." 23:59:59'
	  ";
	  $dbSet->open($query);
	  $result = $dbSet->fetchArray();

	  if(isBlank($result['news_id']))
	  {
	    $ToDBArr = array();
	    $ToDBArr['news_id'] = $result['id'];
	    $ToDBArr['rating_sum'] = intval($_POST['ans']);
	    $ToDBArr['rating_count'] = 1;
	    makeInsertList($strColumns, $strValues, $ToDBArr);
	    insertItem($_SQL_TABLE['news_param'], $strColumns, $strValues);
	  }
	  else
	  {
	    $ToDBArr = array();
	    $ToDBArr['rating_sum'] = 'rating_sum + '.intval($_POST['ans']);
	    $ToDBArr['rating_count'] = 'rating_count + 1';
	    makeUpdateList($strSet, $ToDBArr, array('rating_sum','rating_count'));
	    updateItem($_SQL_TABLE['news_param'], $strSet, 'news_id', $result['id']);
	  }

	  go_to($SameURL);
	  exit;
	}

    $listInfo = array();
    $listInfo['page'] = 1;
    $listInfo['onpage'] = 1;
    $listInfo['sort'] = 'date';
    $listInfo['order'] = 'p_date';
    $listInfo['order_type'] = 'DESC';
    $listInfo['where'][]['forSQL'] = "n.linkname = ".$dbSet->quoteValue($pct->getParam('news'));
    $listInfo['where'][]['forSQL'] = "n.p_date >= '".$pct->getParam('year')."-".$pct->getParam('month')."-".$pct->getParam('day')."' AND n.p_date <= '".$pct->getParam('year')."-".$pct->getParam('month')."-".$pct->getParam('day')." 23:59:59'";
    $News = getNewsPages($listInfo, __TRUE, __TRUE);
    setKeywordsAndRubrics($News);
    $News = reset($News);

    if(intval($News['comments']) > 0)
    {      $query = "
      			SELECT c.id, c.user AS userId, c.date, c.body".__FLANG." AS body, c1.body".__FLANG." AS root, u.login AS user, c1.id AS rootId, c1.date AS rootDate, c1.user AS rootUserId, u1.login AS rootUser
      			FROM ".$_SQL_TABLE['comment']." c
      			INNER JOIN ".$_SQL_TABLE['user']." u ON u.id = c.user
      			LEFT JOIN ".$_SQL_TABLE['comment']." c1 ON c1.id = c.root AND c1.active = 'y'
      			LEFT JOIN ".$_SQL_TABLE['user']." u1 ON u1.id = c1.user
      			WHERE
      					c.active = 'y'
      				AND	u.active = 'y'
      				AND c.news_id = '".$News['id']."'
                ORDER BY c.date
                ".(($_GET['show'] != 'all') ?
                "LIMIT ".$CONFIG['CommentsCountDefault'] :
                ""
                )."
      ";
      $dbSet->open($query);
      $News['comments_list'] = $dbSet->fetchRowsAll();
    }

    $Keywords = array();
    foreach($News['keywords_arr'] as $n => $arr)
    {      $Keywords[] = $arr['title'];
    }

    $tpl->assign("MetaKeywords", $News['meta_keywords'].", ".join(', ',$Keywords));
    $tpl->assign("MetaDescription", $News['meta_description'].". ".join('. ',$Keywords));
    $tpl->assign("MetaTitle", $News['meta_title']);

    $tpl->assign("curr", $News);
    $tpl->assign("ErrorMessages", $ErrorMessages);
    $tpl->assign("SameURL", $SameURL);
    $tpl->assign("PageTitleRubric", $News['rubrics_arr'][0]['title']);
    $tpl->assign("PageTitle", $News['title']);
    $tpl->assign("PageFlag", '1');
  }
  elseif(!isBlank($pct->getParam('year')) && !isBlank($pct->getParam('month')) && !isBlank($pct->getParam('day')) && isBlank($pct->getParam('news')))
  {    $NewsList = newsPagesHandler(array("n.p_date >= '".$pct->getParam('year')."-".$pct->getParam('month')."-".$pct->getParam('day')."' AND n.p_date <= '".$pct->getParam('year')."-".$pct->getParam('month')."-".$pct->getParam('day')." 23:59:59'"), $_GET, __FALSE, __TRUE);

    $tpl->assign("News", $NewsList['News']);
    $tpl->assign("Paging", $NewsList['Paging']);
    $tpl->assign("PageTitle", $language['front']['news'].' '.$pct->getParam('day').'.'.$pct->getParam('month').'.'.$pct->getParam('year'));
    $tpl->assign("PageFlag", '2');
  }
  elseif(!isBlank($pct->getParam('year')) && !isBlank($pct->getParam('month')) && isBlank($pct->getParam('day')) && isBlank($pct->getParam('news')))
  {
    $NewsList = newsPagesHandler(array("n.p_date >= '".$pct->getParam('year')."-".$pct->getParam('month')."' AND n.p_date < '".date("Y-m-d", mktime(0, 0, 0, (intval($pct->getParam('month'),10)+1), 1, $pct->getParam('year')))."'"), $_GET, __FALSE, __TRUE);

    $tpl->assign("News", $NewsList['News']);
    $tpl->assign("Paging", $NewsList['Paging']);
    $tpl->assign("PageTitle", $language['front']['news'].' '.$pct->getParam('month').'.'.$pct->getParam('year'));
    $tpl->assign("PageFlag", '2');
  }
  else
  {
    pageNotFound();
  }


  $_BODY_SMARTY_TEMPLATE = $CONFIG['ModulesFolder']."module.news.tpl";

?>